<?php

class DisreasonApp extends MemberbaseApp
{
	var $_disreason_type_mod;
    function __construct()
    {
        $this->DisreasonApp();
    }
    function DisreasonApp()
    {
        parent::__construct();
		$this->check_powers(USER_ADMIN);
		$this->_disreason_type_mod =& m('disreason_type');
    }
	
    function index()
    {

		$page = $this->_get_page();
		$distypes = $this->_disreason_type_mod->find(array(
			'order' => 'did desc',
			'count' => true,
			'limit' => $page['limit']
		));
		$page['item_count'] = $this->_disreason_type_mod->getCount();
		$this->_format_page($page);
		$this->assign('page_info', $page);
		$this->assign('distypes', $distypes);
        $this->display('disreason.index.html');
        /* 当前位置 */
        $this->_curlocal(LANG::get('折扣原因'));
    }
	
	function add()
	{
		if (!IS_POST) {
			$this->_assign_form();
	        $this->display('disreason.form.html');
        	/* 当前位置 */
        	$this->_curlocal(LANG::get('折扣原因'), 'index.php?app=disreason',
				Lang::get('添加折扣原因'));
		} else {
			$type_name = trim($_POST['type_name']);
			
			if (empty($type_name)) {
				$this->show_warning('折扣原因名称不能为空');
				return;
			}
			if (!$this->_disreason_type_mod->unique($type_name)) {
				$this->show_warning('折扣原因名称已存在');
				return;
			}
			
			$data = array(
				'type_name' => $type_name,
			);
			
			$did = $this->_disreason_type_mod->add($data);
			if (!$did) {
				$this->_error($this->_disreason_type_mod->get_error());
				return;
			}
			$this->show_message('添加折扣原因成功', Lang::get('折扣原因管理'), 'index.php?app=disreason');
		}
	}
	
	function _assign_form()
	{
		$brand = array(
			'if_show' => 1,
		);
		$this->assign('brand', $brand);
	}
	
	function edit()
	{
		$did = intval($_GET['did']);
		if (!$did) {
			$this->show_warning('折扣原因不存在');
			return;
		}
		if (!IS_POST) {
			$disreason_info = $this->_disreason_type_mod->get($did);
			$this->assign('disreason_info', $disreason_info);
			
	        $this->display('disreason.form.html');
        	/* 当前位置 */
        	$this->_curlocal(LANG::get('折扣原因'), 'index.php?app=disreason',
				Lang::get('编辑折扣原因'));
		} else {
			$type_name = trim($_POST['type_name']);
			
			if (empty($type_name)) {
				$this->show_warning('折扣原因名称不能为空');
				return;
			}
			if (!$this->_disreason_type_mod->unique($type_name,$did)) {
				$this->show_warning('折扣原因名称已存在');
				return;
			}
			
			$data = array(
				'type_name' => $type_name,
			);
			$this->_disreason_type_mod->edit($did, $data);
			if ($this->_disreason_type_mod->get_error()) {
				$this->_error($this->_disreason_type_mod->get_error());
				return;
			}
			$this->show_message('编辑折扣原因成功', Lang::get('折扣原因管理'), 'index.php?app=disreason');
		}
	}

	function del()
	{
		$did = intval($_GET['did']);
		if (!$did) {
			$this->show_warning('折扣原因不存在');
			return;
		}

		$this->_disreason_type_mod->drop("did={$did}");
		if ($this->_disreason_type_mod->get_error()) {
			$this->_error($this->_disreason_type_mod->get_error());
			return;
		}
		
		$this->show_message('删除折扣原因成功','折扣原因列表', 'index.php?app=disreason');
		
	}
		
	function check_type_name()
	{
		$type_name = empty($_GET['type_name']) ? null : trim($_GET['type_name']);
		$did = empty($_GET['did']) ? 0 : intval($_GET['did']);
		if (!$type_name) {
            echo ecm_json_encode(false);
            return;
		}
		echo ecm_json_encode($this->_disreason_type_mod->unique($type_name, $did));
	}
}

?>
